import data from '@/common/data.js'; //筛选菜单数据
import validator from '@/common/validator'
export default {
	data() {
		return {
			// 地域数据
			placeData: null,

			loadShow1: false,
			defaultSelected: [],
			filterData: [],
			keyword: '',
			// 记录页数
			page: 1,
			pageSize: 10,
			// 公司的数量
			allCompany: [],
			allSize: null,
			// 记录总页数
			allPage: null,
			status: 'loadmore',
			// 记录筛选结果
			chooseObj: {},
			// 单独获取省份信息
			areaList: null,
			// 判断字符a是否存在
			wordA: null,
			// 判断字符b是否存在
			wordB: null,
			// 是否出现自动完成框
			textTip: false,
			// 判断当前是哪一个没有数据
			noData: null,
			// 记录下滑该调用哪个搜索函数
			searchFlag: null,
			
			// 控制联想框出现
			textTip2: true
		}
	},
	onLoad: function() {
		this.checkCompany()
		// 从缓存中拿取区域数据
		this.getPlaceData()
		this.filterData = []
		
		let companyType = uni.getStorageSync('filterCompanyType')

		this.filterData.push({
			name: '地区',
			"type": 'hierarchy',
			"submenu": this.placeData
		})

		this.filterData.push({
			"name": '类型',
			"type": 'filter',
			"submenu": [{
				"name": "类型",
				"submenu": companyType
			}]
		})
		this.changeSelected()

	},
	methods: {
		allSearch() {
			
			if (!validator.checkNullValue(this.keyword) || !this.keyword) {
				return;
			}
			this.searchFlag = 1
			this.page = 1
			this.allCompany = []
			this.textTip2 = false
			
			this.noExitData=null
			this.moreSearch()
		},
		moreKeyWord(value) {
			this.keyword = value
		},
		textTipChange(value) {
			this.textTip2 = value
		},
		moreSearch() {
			
			let param = {

				'page': this.page,
				"pageSize": this.pageSize
			}

			if (this.chooseObj.qualification) {
				param.qualification = this.chooseObj.qualification
			}
			if (this.chooseObj.companyProvince) {
				param.companyProvince = this.chooseObj.companyProvince
			}
			if (this.chooseObj.companyCity) {
				param.companyCity = this.chooseObj.companyCity
			}

			if (this.keyword) {
				param.companyName = this.keyword
			}
			this.status = 'loading'
			this.$app.request({
				url: this.$baseApp.api.check.findAllMore,
				data: param,
				method: 'GET',
				dataType: 'json',
				success: res => {
					if (res.code == 200) {
						this.allPage = res.data.totalPages
						this.allSize = res.data.totalElements
						if (this.allPage <= 1 || res.data.content.length < 10) {
							this.status = 'nomore'
						} else {
							this.status = 'loadmore'
						}
						let data=res.data.content
						data.forEach((item, index) => {
						
							if (item.companyHistoryNames) {
								item.companyHistoryNames = item.companyHistoryNames.split("$$$")
								let data2=item.companyHistoryNames
								item.companyHistoryNames=[]
								data2.forEach((item2,index2)=>{
									item2=validator.joinStr(item2,this.keyword)
									item.companyHistoryNames.push(item2)
								})
							}
							if (item.companyRegLocation && item.companyRegLocation.length >= 20) {

								item.companyRegLocation = item.companyRegLocation.slice(0, 20) +"..."
								item.companyRegLocation = validator.joinStr(item.companyRegLocation,this.keyword)
							}

							item.companyName = validator.joinStr(item.companyName, this.keyword)

							this.allCompany.push(item)
						})
						if (!this.allCompany || this.allCompany.length == 0) {
							this.noData = 1
							this.searchFlag = null

						}
					} else {
						this.showTip(res.msg)
						this.noData = 1
					}
				},
				error:()=>{
						this.status = 'nomore'
				},
				complete: res => {
					this.loadShow1 = false
					
				}
			});

		},

		//修改选中项-示例
		changeSelected() {
			//设置选中项
			// 一下的注释是针对测试数据说明结构的意思，具体传入什么数据，要看你自己数据。如果data.js数据有修改，注意defaultSelected也要修改
			//传入defaultSelected的结构不能错，错了就报错运行异常。 不想选中的请传入null

			this.defaultSelected = [];
			this.$nextTick(() => {
				this.defaultSelected = [
					[0, 0], //第0个菜单选中 一级菜单的第6项，二级菜单的第0项，三级菜单的第1项

					[
						[0],
						[1],
						[1]
					] //单选菜单选中 第一个筛选的第0项，第二个筛选的第1项，第三个筛选的第1项
				];
			})
		},
		refreshArr() {
			if (this.page >= this.allPage || this.allCompany.length < 1) {
				this.status = 'nomore';
				return
			}
			this.status = 'loading';
			this.page = ++this.page;
			setTimeout(() => {
				if (this.searchFlag == 1) {
					this.moreSearch()
				} else {
					this.checkCompany()
				}
				if (this.page >= this.allPage) this.status = 'nomore';
				else this.status = 'loading';
			}, 2000)
		},
		getPlaceData() {
			let data = uni.getStorageSync('filterPlaceData')
			this.placeData = data
			this.areaList = []
			let obj = {
				name: '省份',
				value: '省份',
				submenu: [{
					name: "全部",
					value: "全部"
				}]
			}
			data.forEach(item => {
				obj.submenu.push({
					name: item.name,
					value: 'A' + item.name
				})
			})
			this.placeData.unshift(obj)
			this.areaList.push(obj)

		},
		// 搜索
		searchResult(value) {

			this.keyword = value
			this.page = 1
			this.allCompany = []
			this.checkCompany()

		},
		// 查询企业
		checkCompany() {
			this.loadShow1 = true
			this.textTip = false
			let param = {
				'page': this.page,
				"pageSize": this.pageSize
			}

			if (this.chooseObj.qualification) {
				param.qualification = this.chooseObj.qualification
			}
			if (this.chooseObj.companyProvince) {
				param.companyProvince = this.chooseObj.companyProvince
			}
			if (this.chooseObj.companyCity) {
				param.companyCity = this.chooseObj.companyCity
			}

			if (this.keyword) {
				param.companyName = this.keyword
			}
			this.searchFlag = null
			this.noData = null
			this.status = 'loading'
			this.$app.request({
				url: this.$baseApp.api.dicData.findAll,
				data: param,
				method: 'GET',
				dataType: 'json',
				success: res => {
					if (res.code == 200) {
						this.allPage = res.data.totalPages
						this.allSize = res.data.totalElements
						if (this.allPage <= 1 || res.data.content.length < 10) {
							this.status = 'nomore'
						} else {
							this.status = 'loadmore'
						}
						res.data.content.forEach((item, index) => {
							if (item.companyRegLocation && item.companyRegLocation.length >= 20) {

								item.companyRegLocation = item.companyRegLocation.slice(0, 20) +
									"..."
								item.companyRegLocation = validator.joinStr(item.companyRegLocation,
									this
									.keyword)
							}

							item.companyName = validator.joinStr(item.companyName, this.keyword)

							if (item.companyHistoryNames) {
								item.companyHistoryNames = item.companyHistoryNames.split("$$$")
								let data2=item.companyHistoryNames
								item.companyHistoryNames=[]
								data2.forEach((item2,index2)=>{
									item2=validator.joinStr(item2,this.keyword)
									item.companyHistoryNames.push(item2)
								})
							}
							this.allCompany.push(item)
						})
					} else {
						this.showTip(res.msg)
					}
				},
				complete: res => {
					this.loadShow1 = false
					uni.stopPullDownRefresh();
					uni.hideLoading();
				}
			});
		},
		//接收菜单结果
		confirm(e) {
			this.indexArr = e.index;
			this.valueArr = e.value;
			// console.log("this.valueArr",this.valueArr)
			// console.log("this.valueArr",this.valueArr)
			this.chooseObj = {}

			this.wordA = this.valueArr[0][1].indexOf('A')
			this.wordB = this.valueArr[0][1].indexOf('B')

			if (this.valueArr[0][1] && this.valueArr[0][1] == 'B市辖区') {
				this.chooseObj.companyProvince = this.valueArr[0][0].slice(1)

			} else {
				if (this.wordA > -1) {
					this.chooseObj.companyProvince = this.valueArr[0][1].slice(1)
				}
				if (this.wordB > -1) {
					this.chooseObj.companyCity = this.valueArr[0][1].slice(1)
				}
				this.wordA = null
				this.wordB = null
			}

			this.valueArr[1][0].forEach(item => {
				if (this.chooseObj.qualification) {
					this.chooseObj.qualification += ',' + item.code
				} else {
					this.chooseObj.qualification = item.code
				}
			})

		

			this.allCompany = []
			this.checkCompany()
		}

	}
}
